name: Linux Tests

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  release:
    types: [published]

jobs:
  linuxtests:
    name: Linux
    strategy:
      matrix:
        group: [integrity, integrity2, integrity3, release_test, docs, usage, usage2, splice_source, python, examples, interop, nonode, lint]
        # This will be used by the base setup action
        python-version: ["3.8", "3.11"]
        include:
          - group: examples
            upload-output: true
          - group: release_test
            upload-output: true
        exclude:
          - group: integrity
            python-version: "3.8"
          - group: integrity2
            python-version: "3.8"
          - group: integrity3
            python-version: "3.8"
          - group: release_test
            python-version: "3.8"
          - group: docs
            python-version: "3.8"
          - group: usage
            python-version: "3.8"
          - group: usage2
            python-version: "3.8"
          - group: nonode
            python-version: "3.8"
          - group: lint
            python-version: "3.8"
          - group: examples
            python-version: "3.8"
          - group: splice_source
            python-version: "3.8"
      fail-fast: false
    timeout-minutes: 45
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1

      - name: Setup firefox
        uses: browser-actions/setup-firefox@latest

      - name: Install dependencies
        env:
          GROUP: ${{ matrix.group }}
        run: |
          bash ./scripts/ci_install.sh

      - name: Run test ${{ matrix.group }}
        env:
          GROUP: ${{ matrix.group }}
          JLAB_BROWSER_TYPE: firefox
        run: |
          bash ./scripts/ci_script.sh

      - name: Upload ${{ matrix.group }} results
        if: ${{ matrix.upload-output && always() }}
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.group }} ${{ github.run_number }}
          path: |
            ./build/${{ matrix.group }}_output
            ./examples/*/test-results

  test_minimum_versions:
    name: Test Minimum Versions
    timeout-minutes: 30
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
        with:
          python_version: "3.8"
      - name: Install minimum versions
        uses: jupyterlab/maintainer-tools/.github/actions/install-minimums@v1
      - name: Install dependencies
        run: |
          bash ./scripts/ci_install.sh
      - name: Run the unit tests
        run: pytest -vv || pytest -vv --lf

  make_sdist:
    name: Make SDist
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4
      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Install dependencies
        run: |
          bash ./scripts/ci_install.sh
      - name: Build SDist
        run: |
          pip install build
          python -m build --sdist
      - uses: actions/upload-artifact@v4
        with:
          name: "sdist"
          path: dist/*.tar.gz

  test_sdist:
    runs-on: ubuntu-latest
    needs: [make_sdist]
    name: Install from SDist and Test
    timeout-minutes: 20
    steps:
      - name: Base Setup
        uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - name: Download sdist
        uses: actions/download-artifact@v4
      - name: Install From SDist
        run: |
          set -ex
          cd sdist
          mkdir test
          tar --strip-components=1 -zxvf *.tar.gz -C ./test
          cd test
          pip install -e .[dev,test]
          pip install pytest-github-actions-annotate-failures
      - name: Run Test
        run: |
          cd sdist/test
          pytest -vv || pytest -vv --lf

  check_links:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - run: |
          node ./jupyterlab/staging/yarn.js install
          node ./jupyterlab/staging/yarn.js run build:packages
          node ./jupyterlab/staging/yarn.js run docs
      - uses: jupyterlab/maintainer-tools/.github/actions/check-links@v1
        with:
          ignore_glob: "docs/api packages/ui-components/docs/source/ui_components.rst images"
          ignore_links: ".*/images/[\\w-]+.png https://docs.github.com/en/.* https://jupyterlab.github.io https://mybinder.org/v2/gh/jupyterlab/.*"
